package com.example.task2.customer;

import com.example.task2.exception.ApiRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@RestController

@RequestMapping(path = "api/v2/customers")
public class CustomerController2 {
   private final CustomerService cs;
  @Autowired
    public CustomerController2(CustomerService cs) {
        this.cs=cs;
    }

    @GetMapping
    List<Customer> getCustomers(){
        return cs.getCustomers();
    }
    @GetMapping(path="{customerId}")
    Customer getCustomer(@PathVariable("customerId") Long id){
        return cs.getCustomer(id);
    }


    @GetMapping(path="{customerId}/exception")
    Customer GetCustomerException(@PathVariable("customerId") Long id){
          throw new ApiRequestException(
                  "ApiRequestException for customer "+id);
    }


    @PostMapping
    void createNewCustomer(@Valid @RequestBody Customer customer){
        System.out.println("post...请求");
        System.out.println(customer);
    }
    @PutMapping
    void updateCustomer(@RequestBody Customer customer){
        System.out.println("UPDATE 请求");
        System.out.println(customer);
    }

    @DeleteMapping(path="{customerId}")
    void deleteCustomer(@PathVariable("customerId") Long id){

      System.out.println("DELETE 请求，删除 Customer ,其ID为" +id);
    }
}
